﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using webcam;
using System.IO;
using WhiteBoard;

namespace WFFS
{
    public partial class Camera : Form
    {
        public Camera()
        {
            InitializeComponent();
        }
        WCamera pick;
        public Form1 form;
        private void Camera_Load(object sender, EventArgs e)
        {
            pick = new WCamera(this.image.Handle, 0,0, this.image.Size.Width, this.image.Size.Height);
            pick.Start();
        }
        Bitmap Bi;
        private void button1_Click(object sender, EventArgs e)
        {

            if (this.button1.Text.Equals("拍照")){
                button1.Text = "继续";
                String path ="c:\\a.bmp";
                pick.GrabImage(path);
              
                Bi = new Bitmap(path);
               this.pictureBox1.Image = Bi;
                 this.image.Visible = false;
                 this.pictureBox1.Visible = true;
                 if(this.form.netWork!=null){
                     System.IO.MemoryStream Ms = new MemoryStream(); 
                     this.pictureBox1.Image.Save(Ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
                     byte[] img = new byte[Ms.Length];
                     Ms.Position = 0; Ms.Read(img, 0, Convert.ToInt32(Ms.Length));
                     Ms.Close();
                     WBMsgImageUpdate wn = new WBMsgImageUpdate();
                     wn.im = img;
                     System.DateTime currentTime = System.DateTime.Now;
                     String name = currentTime.Year.ToString() + currentTime.Month.ToString() + currentTime.Day.ToString() + currentTime.Hour.ToString() + currentTime.Minute.ToString() + currentTime.Second.ToString() + ".bmp";
                     wn.name = name;
                     form.netWork.SendWBMsgConnectedHdlr(wn);
                 }
            }else {
                button1.Text = "拍照";
                this.pictureBox1.Image = null;
                this.pictureBox1.Dispose();
                this.pictureBox1 = new PictureBox();
                Bi.Dispose();
                Bi = null;
                this.image.Visible = true;
                this.pictureBox1.Visible = false;
            }
        }
    }
}
